package de.ret_world.openrpgstudio.scripteditor;

import buoy.event.CommandEvent;
import buoy.event.SelectionChangedEvent;
import buoy.event.WindowClosingEvent;
import buoy.widget.*;
import de.ret_world.openrpgstudio.editor.Project;
import de.ret_world.openrpgstudio.editor.gui.ScriptArea;
import de.ret_world.openrpgstudio.editor.helpers.Internationalization;
import de.ret_world.openrpgstudio.editor.helpers.ScriptTools;
import de.ret_world.openrpgstudio.editor.plugininterfaces.ToolPlugin;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.nio.file.Path;
import java.util.ArrayList;

/**
 * Main class for the ScriptEditor Plugin
 *
 * @author Emu
 */
public class ScriptEditor extends ToolPlugin {

    /**
     * This plug-in ID.
     */
    public static final String PLUGIN_ID = "de.ret_world.openrpgstudio.scripteditor";
    //Variables
    private BFrame parent;
    private BDialog dialog;
    private BSplitPane splitpane;
    private BList slist;
    private ScriptArea script;

    @Override
    public void init(BMenuItem menu, BButton button, BFrame parent) {
        this.parent = parent;

        menu.addEventLink(CommandEvent.class, this, "openEditor");
        button.addEventLink(CommandEvent.class, this, "openEditor");
    }

    private void openEditor() {
        if (dialog == null) {
            Internationalization intn = Internationalization.getInstance();

            int x = Integer.parseInt(config.getProperty("dialog.loc.x", "150"));
            int y = Integer.parseInt(config.getProperty("dialog.loc.y", "150"));
            int w = Integer.parseInt(config.getProperty("dialog.size.w", "750"));
            int h = Integer.parseInt(config.getProperty("dialog.size.h", "500"));

            dialog = new BDialog(parent, intn.getString("SE.SCRIPTEDITOR"), true);
            dialog.setBounds(new Rectangle(x, y, w, h));
            dialog.addEventLink(WindowClosingEvent.class, this, "closeDialog");

            splitpane = new BSplitPane(BSplitPane.HORIZONTAL);
            splitpane.setDividerLocation(Integer.parseInt(config.getProperty("dialog.divider", "200")));

            script = new ScriptArea();
            splitpane.add(script, 1);

            LayoutInfo li = new LayoutInfo(LayoutInfo.WEST, LayoutInfo.HORIZONTAL, new Insets(5, 5, 5, 5), null);

            BorderContainer bc = new BorderContainer();

            bc.add(new BLabel(intn.getString("SE.SCRIPTS")), BorderContainer.NORTH, li);
            slist = new BList();
            slist.addEventLink(SelectionChangedEvent.class, this, "selectionChanged");
            BScrollPane scrl = new BScrollPane(BScrollPane.SCROLLBAR_NEVER, BScrollPane.SCROLLBAR_AS_NEEDED);
            scrl.setContent(slist);
            scrl.setForceHeight(true);
            bc.add(BOutline.createEtchedBorder(scrl, false), BorderContainer.CENTER, new LayoutInfo(LayoutInfo.CENTER, LayoutInfo.BOTH, new Insets(5, 5, 5, 5), null));

            BorderContainer bc2 = new BorderContainer();
            BButton btn = new BButton(intn.getString("SAVE"));
            btn.addEventLink(CommandEvent.class, this, "save");
            bc2.add(btn, BorderContainer.EAST, li);
            btn = new BButton(intn.getString("CLOSE"));
            btn.addEventLink(CommandEvent.class, this, "closeDialog");
            bc2.add(btn, BorderContainer.WEST, li);
            bc.add(bc2, BorderContainer.SOUTH, new LayoutInfo(LayoutInfo.EAST, LayoutInfo.HORIZONTAL, new Insets(5, 5, 5, 5), null));

            splitpane.add(bc, 0);

            dialog.setContent(splitpane);
        }

        ArrayList<String> scripts = new ArrayList<>();
        Project project = Project.getInstance();
        scripts.addAll(project.getScriptRegister().getScriptMap().keySet());

        slist.removeAll();
        for (String s : scripts) {
            slist.add(s);
        }

        dialog.setVisible(true);
    }

    private void save() {
        if (slist.getSelectedIndex() >= 0) {
            Project project = Project.getInstance();

            String id = (String) slist.getSelectedValue();
            Path file = project.getScriptRegister().getAbsolutePath(id);
            ScriptTools.getInstance().saveScriptFile(file, script.getText());
        }
    }

    private void closeDialog() {
        if (dialog != null) {
            log.info("Saving configuration files");

            config.setProperty("dialog.loc.x", "" + dialog.getBounds().x);
            config.setProperty("dialog.loc.y", "" + dialog.getBounds().y);
            config.setProperty("dialog.size.w", "" + dialog.getBounds().width);
            config.setProperty("dialog.size.h", "" + dialog.getBounds().height);
            config.setProperty("dialog.divider", "" + splitpane.getDividerLocation());

            dialog.dispose();
        }
        if (config != null) {
            config.storeConfig();
        }

        ScriptTools.getInstance().executeStartScript();
    }

    private void selectionChanged() {
        Project project = Project.getInstance();

        if (slist.getSelectedIndex() > -1) {
            String id = (String) slist.getSelectedValue();
            Path file = project.getScriptRegister().getAbsolutePath(id);
            String loaded = ScriptTools.getInstance().readScriptFile(file);

            script.setText(loaded);
        }
    }

    @Override
    public Shortcut getShortcut() {
        return new Shortcut(KeyEvent.VK_S, Shortcut.CTRL_MASK | Shortcut.SHIFT_MASK);
    }
}
